import { ConfigEnv, UserConfig, loadEnv } from 'vite';
import { viteMockServe } from 'vite-plugin-mock';
import createVuePlugin from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import svgLoader from 'vite-svg-loader';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
import path from 'path';
import viteCompression from 'vite-plugin-compression';
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';

// import { Plugin as importToCDN } from 'vite-plugin-cdn-import';

const CWD = process.cwd();
// "vue-pdf-embed": "^1.1.5", //如果加速失效就启用次配置
// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfig => {
  const { VITE_BASE_URL } = loadEnv(mode, CWD);
  return {
    base: VITE_BASE_URL,
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${path.resolve('src/style/variables.less')}";`,
          },
          math: 'strict',
          javascriptEnabled: true,
        },
      },
    },
    esbuild: {
      pure: ['console.log', 'debugger'],
    },
    build: {
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            //静态资源分拆打包
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          },
        },
      },

      target: 'es2015', // �����������
      cssTarget: 'chrome79', // ��ѡ�������û�Ϊ CSS ��ѹ������һ����ͬ������� target
      chunkSizeWarningLimit: 5000, // chunk ��С��������ƣ��� kbs Ϊ��λ����
      outDir: 'dist', // ָ�����·��
      sourcemap: false, //是否构建source map 文件
      // terserOptions: {
      //   // 生产环境移除console
      //   compress: {
      //     drop_console: true,
      //     drop_debugger: true,
      //   },
      // },
      assetsDir: 'static',
      manifest: false,
    },
    plugins: [
      // importToCDN({
      //   modules: [
      //     // {
      //     //   name: 'vue-pdf-embed',
      //     //   var: 'vue-pdf-embed',
      //     //   path: `//static.jiyi.show/assets/minxin/cdn.jsdelivr.net_npm_vue-pdf-embed%401.1.6.js`,
      //     // },
      //   ],
      // }),
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
        ext: '.gz',
      }),
      VueSetupExtend(),
      createVuePlugin(),
      vueJsx(),
      // viteMockServe({
      //   mockPath: 'mock',
      //   localEnabled: true,
      // }),
      svgLoader(),
      // vue(),
      Components({
        resolvers: [VantResolver()],
      }),
    ],
    server: {
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
      port: 8880,
      host: '0.0.0.0',
      proxy: {
        '/api': {
          target: 'http://taniayi.elev.top',
          // 如果你想代理到 https://cqshop.360daypay.com/somepath 下，可以这样设置 pathRewrite  
          // pathRewrite: {'^/api': '/somepath'},  
          changeOrigin: true, // 必须的，因为API的URL和你的开发URL不匹配  
          secure: false, // 如果是https接口，需要配置这个参数  
          // 如果后端接口需要认证，可以添加 headers 配置  
          // headers: {  
          //   'Authorization': 'Bearer your_token_here'  
          // }  
        },
      },
    },
  };
};
